function onModuleLoad() {
	var $stateModule = $("#stateModule");
	var $state_add_form = $( "#stateModule_add_form" , $stateModule);
	var $serverErrormessage = $( "#stateModule_delete_dialog_status" , $state_delete_dialog);
	var deleteStateName;
	var deleteStateUrl;
	var deleteStateId;
	var stateRowToDelete;
	$("#accordion").accordion('activate', 1);
	// following script stops the submit event of form when user clicks the Enter button
	$('.noEnterSubmit').keypress(function(e){
	  	if ( e.which == 13 ) return false;
	  	
	});
	var $state_add_dialog = $( "#stateModule_add_dialog", $stateModule ).dialog({
		autoOpen: false,
		modal: true,
		autoOpen: false,
		width: 440,
		buttons: {
			Add: function() {
				if($state_add_form.valid())
				{
				    var url = $state_add_form.attr( 'action' );
				   	$.post(url,$state_add_form.serialize(), function(data) {
				   			
							  if(data.status == "Success"){
					    	  $state_add_dialog.dialog( "close" );
					    	  $stateTable.append("<tr> <td>" +data.resultMap.newStateName + "</td> <td><a class=\"stateModuleDeleteState\" href=\"../state/delete/$" +data.resultMap.newStateId +"\">Delete</a></td> </tr>");
					      }
					      else {
					    	  $serverErrormessage = $( "#stateModule_add_dialog_status" , $state_add_dialog);
					    	  $serverErrormessage.text("Error adding new state - " + data.status);
					      }					   	  
					    }, "json");					
				}
			},
			Cancel: function() {
				$( this ).dialog( "close" );
			}
		},
		open: function() {
			$tab_title_input.focus();
		},
		close: function() {
			$state_add_form[ 0 ].reset();
			$serverErrormessage.text("");
		}
	});
	
	var $state_delete_dialog = $("#stateModule_delete_dialog", $stateModule ).dialog({
		autoOpen: false,
		modal: true,
		autoOpen: false,
		width: 440,
		buttons: {
			Yes: function() {
				$.post(deleteStateUrl,{stateId:deleteStateId}, function(data) {
						  if(data.status == "Success"){
				    	  $state_delete_dialog.dialog( "close" );
				    	  stateRowToDelete.remove();
				      }
				      else {
				    	  
				    	  $serverErrormessage.text("Error deleting the state - " + data.errorMap.errorMessage);
				      }  
				    }, "json")
				    ;		
						
			},
			No: function() {
				$( this ).dialog( "close" );
			}
		},
		open: function() {
			$tab_title_input.focus();
		},
		close: function() {
			$serverErrormessage.text("");
		}
	});
	// addTab button: just opens the dialog
	$( "#stateModule_add_button", $stateModule ).click(function() {
			$state_add_dialog.dialog( "open" );
		});
	
	var $stateTable = $("#stateModule_table",$stateModule);
	var $stateDeleteLink = $("a", $stateTable);
	$stateDeleteLink.live("click",function(event) {
        event.preventDefault();
        $serverErrormessage.text("");
        stateRowToDelete = $(this).parent().parent();
        deleteStateName = $(this).parent().siblings().html();
        deleteStateUrl = $(this).attr('href');
        var hrefArray= deleteStateUrl.split('$'); 
        deleteStateUrl = hrefArray[0];
        deleteStateId = hrefArray[1];
        $("#stateModule_delete_message",$state_delete_dialog).text(deleteStateName);
        $state_delete_dialog.dialog( "open" );
       
    });
};
	
